﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

using System.Net.Mail;
using System.Net;
using System.Threading;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class product_card_admin_ApplicationPending : System.Web.UI.Page
{

      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  
    protected void Page_Load(object sender, EventArgs e)
    {
        String CustID = Session["custID"] as String;




    }
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection myConnection = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT * from ApplicationDebit ";

        SqlDataAdapter myAdapter = new SqlDataAdapter(strCommandText, myConnection);


        DataSet myDS = new DataSet();

        myAdapter.Fill(myDS, "ApplicationInfo");

        gvPlayers.DataSource = myDS;

        gvPlayers.DataBind();
    }



    protected void Button3_Click(object sender, EventArgs e)
    {

        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;

        smtp.Credentials = new NetworkCredential(TextBox8.Text, TextBox9.Text);
        MailMessage mail = new MailMessage(TextBox8.Text, TextBox1.Text, TextBox3.Text, TextBox4.Text);
        try
        {
            if (fileUpload1.HasFile)
            {
                mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
            }
            smtp.Send(mail);

        }
        catch (Exception ex)
        {
            Response.Write("Exception Occured : " + ex);

        }
    }

    public object CustID { get; set; }
    public object Email { get; set; }
    protected void Button4_Click(object sender, EventArgs e)
    {
        con.Open();
         var gengerator = new AccountsManager();
        var chars = "0123456789";
        var random = new Random();
        var result = new string(
            Enumerable.Repeat(chars, 8)
                      .Select(s => s[random.Next(s.Length)])
                      .ToArray());
        string hashresult = FormsAuthentication.HashPasswordForStoringInConfigFile(result, "MD5");
        //string AccountNo = gengerator.GenerateNo("CARDS");
        string card_no = gengerator.GenerateNo("CARDS");

        SqlCommand myCommands = new SqlCommand("INSERT INTO ACCOUNTS(custID, accountNo, type) " +
        "VALUES (@custID, @accountNo,  @type)", con);
        myCommands.Parameters.AddWithValue("@custID", DropDownList1.SelectedItem.Text);
        myCommands.Parameters.AddWithValue("@accountNo", card_no);
        myCommands.Parameters.AddWithValue("@type", "CARDS");
        myCommands.ExecuteNonQuery();
        SqlCommand cmd6 = new SqlCommand("UPDATE APPLICATIONDEBIT SET status = @status WHERE custID = @custID", con);
        cmd6.Parameters.Add("@status", Label5.Text);
        cmd6.Parameters.Add("@custID", DropDownList1.SelectedItem.Text);
        cmd6.ExecuteNonQuery();
       decimal  limit = 500;

        SqlCommand cmd1 = new SqlCommand("insert into CHARTSPENDINGS values('" + DropDownList1.SelectedItem + "','" + limit + "','" +TextBox10.Text+ "')", con);
        cmd1.ExecuteNonQuery();


       

        String sql = " insert into CARDGENERATOR (cardNo,custID,PIN) values (@cardNo,@custID,@PIN)  ";
        SqlCommand cmd = new SqlCommand(sql, con);

        cmd.Parameters.AddWithValue("@cardNo", card_no);
        cmd.Parameters.AddWithValue("@custID", DropDownList1.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@PIN", result);
     
        cmd.ExecuteNonQuery();

        Alert alert = new Alert();
        alert.addAlertApplication(DropDownList1.SelectedItem.Text, "Your DEBIT CARD application has been approved.", "../product/card/user/OnlineSales.aspx");

        Session["limitLeft"] = limit;
        TextBox6.Text = "Successful debit card application";
        TextBox7.Text = "Your card number is :"+card_no+" and your password is :"+result;
        Session["limit"] = limit;
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;

        smtp.Credentials = new NetworkCredential(TextBox8.Text, TextBox9.Text);
        MailMessage mail = new MailMessage(TextBox8.Text, TextBox1.Text, TextBox6.Text, TextBox7.Text);
        
        
        try
        {
            if (fileUpload1.HasFile)
            {
                mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
            }
            smtp.Send(mail);

        }
        catch (Exception ex)
        {
            Response.Write("Exception Occured : " + ex);

        }
    }



}